package com.test.textGame.mq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class RocketMqSlaverConsumer {

    @Value("${rocketmq.slaver.consumer.namesrvAddr}")
    private String namesrvAddr;
    @Value("${rocketmq.slaver.consumer.groupName}")
    private String groupName;
    @Value("${rocketmq.slaver.consumer.topics}")
    private String topics;

    private DefaultMQPushConsumer consumer;

    @Autowired
    RocketMqSlaverListener rocketMqSlaverListener;
    
    @PostConstruct
    public void init() throws MQClientException {
        consumer = new DefaultMQPushConsumer(groupName);
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.subscribe(topics,"*");
        consumer.registerMessageListener(rocketMqSlaverListener);

        // 16次失败 就进私信队列
        consumer.setMaxReconsumeTimes(16);
        consumer.start();
    }
}